package com.ruoyi.partner.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.partner.domain.Partners;
import com.ruoyi.partner.mapper.PartnersMapper;
import com.ruoyi.partner.service.IPartnersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 城市合伙人Controller
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/partner/partner")
public class PartnersController extends BaseController {
    @Autowired
    private IPartnersService partnersService;
    @Resource
    private PartnersMapper partnersMapper;

    /**
     * 查询城市合伙人列表
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:list')")
    @GetMapping("/list")
    public TableDataInfo list(Partners partners) {
        startPage();
        List<Partners> list = partnersService.selectPartnersList(partners);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (Partners partners1 : list) {
                String userName = partnersMapper.selectUserNameById(partners1.getUserId());
                partners1.setUserName(!CheckEmptyUtil.isEmpty(userName)?userName:"");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出城市合伙人列表
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:export')")
    @Log(title = "城市合伙人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Partners partners) {
        List<Partners> list = partnersService.selectPartnersList(partners);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (Partners partners1 : list) {
                String userName = partnersMapper.selectUserNameById(partners1.getUserId());
                partners1.setUserName(!CheckEmptyUtil.isEmpty(userName)?userName:"");
            }
        }
        ExcelUtil<Partners> util = new ExcelUtil<Partners>(Partners.class);
        util.exportExcel(response, list, "城市合伙人数据");
    }

    /**
     * 获取城市合伙人详细信息
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(partnersService.selectPartnersById(id));
    }

    /**
     * 新增城市合伙人
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:add')")
    @Log(title = "城市合伙人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Partners partners) {
        return toAjax(partnersService.insertPartners(partners));
    }

    /**
     * 修改城市合伙人
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:edit')")
    @Log(title = "城市合伙人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Partners partners) {
        return toAjax(partnersService.updatePartners(partners));
    }

    /**
     * 删除城市合伙人
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:remove')")
    @Log(title = "城市合伙人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(partnersService.deletePartnersByIds(ids));
    }
}
